package cn.ijero.pictureselector.util;

import android.util.Log;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeSet;

import cn.ijero.pictureselector.data.GroupLocalMedia;
import cn.ijero.pictureselector.data.LocalMedia;

public class ConvertUtil {
    private static final String TAG = "ConvertUtil";
    private static final String NOT_GROUP = "##";

    public static ArrayList<GroupLocalMedia> groupLocalMedias(ArrayList<LocalMedia> localMedias) {
        ArrayList<GroupLocalMedia> result = new ArrayList<>();
        HashMap<String, List<GroupLocalMedia>> mediaHashMap = new HashMap<>();
        mediaHashMap.put(NOT_GROUP, new ArrayList<>());

        for (LocalMedia localMedia : localMedias) {
            if (localMedia.getDatetime() != null && !localMedia.getDatetime().isEmpty()) {
                String[] split = localMedia.getDatetime().split(" ");
                if (split.length > 0) {
                    String group = split[0];
                    List<GroupLocalMedia> groupLocalMedias = mediaHashMap.get(group);
                    if (groupLocalMedias == null) {
                        groupLocalMedias = new ArrayList<>();
                        //  分组标题头
                        GroupLocalMedia groupLocalMedia = new GroupLocalMedia(true, group, null);
                        groupLocalMedias.add(groupLocalMedia);
                    }
                    // 条目
                    GroupLocalMedia groupLocalMedia = new GroupLocalMedia(false, group, localMedia);
                    groupLocalMedias.add(groupLocalMedia);
                    mediaHashMap.put(group, groupLocalMedias);
                } else {
                    List<GroupLocalMedia> groupLocalMedias = mediaHashMap.get(NOT_GROUP);
                    GroupLocalMedia groupLocalMedia = new GroupLocalMedia(false, NOT_GROUP, localMedia);
                    groupLocalMedias.add(groupLocalMedia);
                    mediaHashMap.put(NOT_GROUP, groupLocalMedias);
                }
            }
        }
        Set<String> keySet = mediaHashMap.keySet();
        NavigableSet<String> descendingSet = new TreeSet<>(keySet).descendingSet();
        for (String next : descendingSet) {
            List<GroupLocalMedia> list = mediaHashMap.get(next);
            if (list != null) {
                list.sort(new Comparator<GroupLocalMedia>() {
                    @Override
                    public int compare(GroupLocalMedia o1, GroupLocalMedia o2) {
                        LocalMedia localMedia1 = o1.getLocalMedia();
                        LocalMedia localMedia2 = o2.getLocalMedia();
                        if (localMedia1 == null || localMedia2 == null) {
                            return 0;
                        }
                        return localMedia1.getDatetime().compareTo(localMedia2.getDatetime());
                    }
                });
                result.addAll(list);
            }
            Log.d(TAG, "groupLocalMedias: ");
        }
        return result;
    }
}
